package online.hxstart.code.concurrent.chapter04;

import java.util.concurrent.TimeUnit;

/**
 * @author huxing
 * @date 19-4-24 星期三 上午11:29
 * @describe
 */
public class ThisMonitor {
  public synchronized void method1() {
    System.out.println(Thread.currentThread().getName() + " enter to method1");
    try {
      TimeUnit.MINUTES.sleep(10);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public void method2() {
    synchronized (this) {
      System.out.println(Thread.currentThread().getName() + " enter to method2");
      try {
        TimeUnit.MINUTES.sleep(10);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }

  public void sayHello() {
    System.out.println("hello");
  }

  public static void main(String[] args) {
    ThisMonitor thisMonitor = new ThisMonitor();
    ThisMonitor thisMonitor1 = new ThisMonitor();
    new Thread(thisMonitor::method1).start();
    new Thread(thisMonitor::method2).start();
    new Thread(thisMonitor1::method1).start();
    new Thread(thisMonitor1::method2).start();
    new Thread(thisMonitor::sayHello).start();
  }
}
